from httpUtil import HttpUtil
http = HttpUtil("http://api1.zelfly.com/carManager/assist/analysis/")
def getCarIotId():
    """
    获取项目车队和车辆信息
    @return: 包含项目车队车辆信息的dict
    """
    try:
        null = ''
        projectSiteDict = {}
        projectList = []
        teamList = []
        carList = []
        result = eval(http.get())
        if result != '':
            if result['code'] == 1:
                product_result = result['result']
                for product_id in product_result:
                    projectDict = {'value': product_id, 'label': f'{product_id}号项目', 'children': []}
                    product = product_result[product_id]
                    site_list = product['site']
                    projectSiteDict[product_id] = site_list
                    team_list = product['team']
                    for team in team_list:
                        team_id = team['id']
                        team_name = team['name']
                        timeDict = {'value': team_id, 'label': team_name, 'children': []}
                        car_list = team['car']
                        for car in car_list:
                            car_id = car['carId']
                            car_number = car['carNumber']
                            carDict = {'value': car_id, 'label': car_number}
                            carList.append(carDict)
                        timeDict['children'] = carList
                        carList = []
                        teamList.append(timeDict)
                    projectDict['children'] = teamList
                    teamList = []
                    projectList.append(projectDict)
                return projectList, projectSiteDict
            else:
                return [], {}
        else:
            return [], {}
    except Exception as e:
        return [], {}

def saveJson(projectList, name):
    with open(f'../{name}.json', 'w+', encoding='utf8') as fp:
        fp.write(projectList)


if __name__ == '__main__':
    projectList, projectSiteDict = getCarIotId()
    saveJson(str(projectList).replace("'", '"'), 'options')
    saveJson(str(projectSiteDict).replace("'", '"'), 'sites')